<template >
   <el-dialog :title="title" :close-on-click-modal="false" :before-close="closeDialog" :visible.sync="visible" width="600px" append-to-body dialogDrag>
        <el-form ref="form" :model="form" class="form" :rules="rules" size="small" label-width="100px">
            <el-form-item label="任务名称" prop="jobName"> 
                <el-input v-model="form.jobName" style="width:400px"></el-input>
            </el-form-item>
            <el-form-item label="任务分组" prop="jobGroup"> 
                <el-input v-model="form.jobGroup" style="width:400px"></el-input>
            </el-form-item>  
            <el-form-item label="执行类" prop="jobClassName"> 
                <el-input v-model="form.jobClassName" style="width:400px"></el-input>
            </el-form-item>
            <el-form-item label="Corn表达式" prop="cronExpression"> 
                <el-input v-model="form.cronExpression" style="width:400px"></el-input>
            </el-form-item>
            <el-form-item label="任务描述">
                <el-input type="textarea" v-model="form.remark" style="width: 400px;"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="text"    @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="doSubmit" :loading="loading">确认</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { addJob,editJob,selJob} from '@/api/job'
export default {
    inject:['reload'],
    data() {
        return {
            rowId:'',
            title:'',
            visible: false,
            loading:false,  //确认提交按钮加载中
            form: {jobName:'', jobGroup:'', jobClassName:'' , cronExpression:'' , remark:''},
            rules:{
                jobName:       [{ required: true,message: '任务名称不能为空'}],
                jobGroup:      [{ required: true,message: '任务分组不能为空'}],
                jobClassName:  [{ required: true,message: '执行类不能为空'}],
                cronExpression:[{ required: true,message: 'Cron表达式'}],
            }
        };
    },
    methods: {
        closeDialog(){
            this.$refs['form'].resetFields();
            this.visible = false
            this.loading = false
            this.rowId = ''
            this.form = {jobName:'', jobGroup:'', jobClassName:'' , cronExpression:'' , remark:''}
        },
        init(row,isAdd) {
            this.title = (isAdd ? '新增定时任务':'编辑定时任务')
            !!row && (this.rowId = row.id) && this.findUserById(this.rowId)
            this.$nextTick(() => {
                this.visible = true
            })
        },
        findUserById(id){
            this.apiSelJobById(id)
        },
        doSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$confirm("确定?",'提示',{ type: 'warning'}).then( ()=>{
                        if(this.rowId){
                            this.apiUpdateJob(this.form)
                        }else{
                            this.apiAddJob(this.form)
                        }
                    }).catch(err=>{
                        this.loading =false
                    })
                }
            })
        },
        //==========>请求api接口<==========//
        apiSelJobById(id){
            selJob(id).then(res=>{
                if(res.code === 200){
                    this.form ={...res.data}
                }
            },error=>{
                this.$message.error(res.message)
            })
        },
        apiUpdateJob(job){
            editJob(job).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.$message.error(res.message)
            })
        },
        apiAddJob(job){
            addJob(job).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.$message.error(res.message)
            })
        }
        //==========>请求api接口<==========//
        // getData() {
        //     listDeptTree().then(res => {
        //         this.treeData = res.data;
        //     });
        //     listRole().then(res => {
        //         this.roles = res.data.list;
        //     });
        // },
        // normalizer(node) {
        //     return {
        //         label: node.name,
        //         children: node.children,
        //     }
        // },
    },
};
</script>
<style scoped>

</style>


